package com.bnz.dao.impl;

import com.bnz.dao.UserDao;
import com.bnz.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository // 放入spring容器  @Repository:常放入mapper
public class UserDaoImpl implements UserDao {

    // 引入JdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 查询所有数据
    @Override
    public List<User> findAll() { // 灰色表示可以用lambada表达式代替
        return jdbcTemplate.query("select * from user ", new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                return new User(
                        resultSet.getInt("id"),
                        resultSet.getString("username"),
                        resultSet.getDouble("balance"));
            }
        });
    }

}
